﻿@using System.Threading.Tasks
@inject IJSRuntime JS

<button @attributes="additionalAttributes" @onclick="HandleClick" class="button @currentClassName" disabled="@Disabled">
    @if (IsBusy)
    {
        <div class="button-box">
            <Icon class="button-is-busy" IconName="refresh" />
            @if (!string.IsNullOrEmpty(IsBusyText))
            {
                <span style="padding-top: var(--mp-spacing-2)">@IsBusyText</span>
            }
        </div>
    }
    else
    {
        <div class="overlay"></div>
        <div class="button-box">
            @if (!string.IsNullOrEmpty(IconLeft))
            {
                <Icon IconName="@IconLeft" />
            }
            @if (!string.IsNullOrEmpty(Text))
            {
                <span style="padding-top: var(--mp-spacing-2)">@Text</span>
            }
            @if (!string.IsNullOrEmpty(IconRight))
            {
                <Icon IconName="@IconRight" />
            }
        </div>
    }
</button>

@code {
    [Parameter] public bool Disabled { get; set; } = false;
    [Parameter] public bool IsBusy { get; set; } = false;
    [Parameter] public string IsBusyText { get; set; }
    [Parameter] public string ButtonStyle { get; set; } = "brand";
    [Parameter] public string Size { get; set; } = "medium";
    [Parameter] public string IconLeft { get; set; }
    [Parameter] public string Text { get; set; }
    [Parameter] public string IconRight { get; set; }
    [Parameter] public EventCallback OnClick { get; set; }

    private string currentClassName;
    private Dictionary<string, object> additionalAttributes = new Dictionary<string, object>();

    protected override void OnParametersSet()
    {
        var buttonStyle = $"button-style-{ButtonStyle}";
        var size = $"button-size-{Size}";

        currentClassName = string.IsNullOrEmpty(Text) ? $"{buttonStyle} {size} icon-only" : $"{buttonStyle} {size}";
    }

    private async Task HandleClick()
    {
        if (OnClick.HasDelegate)
        {
            await OnClick.InvokeAsync(null);
        }
    }
}
